package com.sun.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.sun.dao.EmployeeMapper;
import com.sun.pojo.Employee;
import com.sun.service.EmployeeService;
import com.sun.utils.PasswordUtil;
import com.sun.utils.SystemConstant;
import com.sun.utils.UUIDutiles;
import com.sun.vo.EmployeeVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;

@Service
@Transactional
public class EmployeeServiceImpl implements EmployeeService {

    @Autowired
    private EmployeeMapper employeeMapper;

    public Employee findEmployeeByLoginName(String loginName, String password) {
        Employee employee = employeeMapper.findEmployeeByLoginName(loginName);
        if (employee != null) {
            String newPassword = PasswordUtil.md5(password, employee.getSalt(), SystemConstant.PASSWORD_COUNT);
            if (employee.getLoginPwd().equals(newPassword)) {
                return employee;
            }
        }
        return null;
    }

    public int checkDeptHasEmployee(int deptId) {
        return employeeMapper.checkDeptHasEmployee(deptId);
    }

    public int checkRoleHasEmployee(int roleId) {
        return employeeMapper.checkRoleHasEmployee(roleId);
    }

    public PageInfo findEmployeeList(EmployeeVo employeeVo) {
        PageHelper.startPage(employeeVo.getPage(), employeeVo.getLimit());
        List<Employee> employeeList = employeeMapper.findEmployeeList(employeeVo);
        PageInfo pageInfo = new PageInfo(employeeList);
        return pageInfo;
    }

    public int addEmployee(Employee employee, HttpSession session) {
        Employee loginUser = (Employee) session.getAttribute(SystemConstant.LOGINUSER);
        employee.setCreateDate(new Date());
        employee.setSalt(UUIDutiles.getUuid());
        employee.setCreatedBy(loginUser.getId());
        employee.setLoginPwd(PasswordUtil.md5(SystemConstant.DEFAULT_EMPLOYEE_PWD, employee.getSalt(), SystemConstant.PASSWORD_COUNT));
        return employeeMapper.addEmployee(employee);
    }

    public int updateEmployee(Employee employee, HttpSession session) {
        Employee loginUser = (Employee) session.getAttribute(SystemConstant.LOGINUSER);
        employee.setModifyDate(new Date());
        employee.setModifyBy(loginUser.getId());
        return employeeMapper.updateEmployee(employee);
    }

    public int deleteEmployee(int id) {
        int i = employeeMapper.deleteEmployeeAndRole(id);
        return employeeMapper.deleteEmployee(id);
    }

    public int resetPasswordEmployee(int id, HttpSession session) {
        Employee employee = new Employee();
        Employee loginUser = (Employee) session.getAttribute(SystemConstant.LOGINUSER);
        employee.setId(id);
        employee.setModifyBy(loginUser.getId());
        employee.setModifyDate(new Date());
        employee.setSalt(UUIDutiles.getUuid());
        employee.setLoginPwd(PasswordUtil.md5(SystemConstant.DEFAULT_EMPLOYEE_PWD,employee.getSalt(),SystemConstant.PASSWORD_COUNT));
        return employeeMapper.updateEmployee(employee);
    }

    public Boolean addEmployeeAndRole(int id, String checkedRole) {
        try {
            String[] checkedIds = checkedRole.split(",");
            int delRes=employeeMapper.deleteAllRoleAndEmployee(id);
            for (String checkedId : checkedIds) {
                employeeMapper.addEmployeeAndRole(id,checkedId);
            }
            return true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }


}
